package request

import (
	"math"
	"strconv"
	"tizhi_front/model"
	"tizhi_front/nspel"
	"tizhi_front/response"
)

type AddEvaluation struct {
	TokenInfo      response.TokenInfo `json:"token_info"`
	EvaluationInfo EvaluationInfo     `json:"evaluation_info"`
}

type EvaluationInfo struct {
	Title                 string  `json:"title" example:"我的第一次测试记录"`
	Height                float64 `json:"height" example:"170"`
	Weight                float64 `json:"weight" example:"60"`
	VitalCapacity         float64 `json:"vital_capacity" example:"3000"`
	FiftyMeters           float64 `json:"fifty_meters" example:"9.7"`
	SitAndReach           float64 `json:"sit_and_reach" example:"15"`
	OneMinuteRopeSkipping float64 `json:"one_minute_rope_skipping" example:"150"`
	OneMinuteSitUp        float64 `json:"one_minute_sit_up" example:"30"`
}

func (e *EvaluationInfo) Calculate(uid int64) (selfEvaluation model.SelfEvaluation0, err error) {
	student := model.Student9{}
	err = student.SelectById(uid)
	if err != nil {
		return
	}
	selfEvaluation = model.SelfEvaluation0{
		StudentId:             uid,
		Title:                 e.Title,
		Height:                e.Height,
		Weight:                e.Weight,
		VitalCapacity:         e.VitalCapacity,
		FiftyMeters:           e.FiftyMeters,
		SitAndReach:           e.SitAndReach,
		OneMinuteRopeSkipping: e.OneMinuteRopeSkipping,
		OneMinuteSitUp:        e.OneMinuteSitUp,
	}
	if selfEvaluation.Height > 0 {
		selfEvaluation.BodyMassIndex = selfEvaluation.Weight / math.Pow(selfEvaluation.Height/100, 2)
		e.GetSubjectScore("体重指数", &student, &selfEvaluation.BodyMassIndexScore, selfEvaluation.BodyMassIndex, true)
	}
	e.GetSubjectScore("肺活量", &student, &selfEvaluation.VitalCapacityScore, selfEvaluation.VitalCapacity, true)
	e.GetSubjectScore("50米跑", &student, &selfEvaluation.FiftyMetersScore, selfEvaluation.FiftyMeters, false)
	e.GetSubjectScore("坐位体前屈", &student, &selfEvaluation.SitAndReachScore, selfEvaluation.SitAndReach, true)
	e.GetSubjectScore("一分钟跳绳", &student, &selfEvaluation.OneMinuteRopeSkippingScore, selfEvaluation.OneMinuteRopeSkipping, true)
	e.GetSubjectScore("一分钟仰卧起坐", &student, &selfEvaluation.OneMinuteSipUpScore, selfEvaluation.OneMinuteSitUp, true)
	return
}

func (e *EvaluationInfo) GetSubjectScore(subject string, student *model.Student9, score *float64, rawValue float64, big bool) {
	standard := nspel.GetStandard(subject, strconv.Itoa(int(student.Sex)), student.CurrentGrade)
	if big {
		for i := 0; i < len(standard); i++ {
			if rawValue >= standard[i].Range {
				*score = standard[i].Score
				return
			}
		}
	} else {
		for i := 0; i < len(standard); i++ {
			if rawValue <= standard[i].Range {
				*score = standard[i].Score
				return
			}
		}
	}
	*score = 0
	return
}
